package modules.popups
{
	import flash.display.Bitmap;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	
	import asf.core.Application;
	import asf.interfaces.IPopup;
	import asf.utils.Align;
	import asf.utils.FX;
	
	import sample.app.Globals;
	import sample.view.assets.CloseButton;
	import sample.viewassets.pops.SampleBasePop;

	public class Pop1 extends SampleBasePop implements IPopup
	{
		private var app:Application;
		
		private var tf:TextField;
		private var bt:CloseButton;
		
		private var img:Bitmap;
		
		public function Pop1( )
		{
			super( );
			
			alpha = 0;
			
			tf = new TextField( );
			bt = new CloseButton( );
			
			tf.text = "Uma popup";
			
			addChild( tf );
			
			app = Application.getByID( Globals.APP_ID );
			
			graphics.beginFill( 0x006699, 1 );
			graphics.drawRect( 0, 0, 500, 500 );
			
			Align.place( bt, Align.RIGHT, { box: new Rectangle( 0, 0, width, height ) } );
			
			tf.mouseEnabled = false;
			
			bt.addEventListener( MouseEvent.CLICK, click );
			
			addChild( bt );
			
			addEventListener( Event.ADDED_TO_STAGE, added );
		}
		
		private function added( evt:Event ):void
		{
			removeEventListener( Event.ADDED_TO_STAGE, added );
			
			img = popup.dependencies.getBitmap( "image" );
			
			Align.place( img, Align.CENTER + Align.MIDDLE, { box: new Rectangle( 0, 0, width, height ) } );
			
			addChild( img );
		}
		
		private function click( evt:MouseEvent ):void
		{
			app.popups.closePopup( this.popup );
		}
		
		public function open():void
		{
			FX.fadeIn( this );
		}
		
		public function close():void
		{
			FX.fadeOut( this ).queue( this.popup.notifyHideEnd );
		}
		
		public function dispose():void
		{
			ctrace( );
			
			removeChild( tf );
			removeChild( bt );
			
			bt.removeEventListener( MouseEvent.CLICK, click );
			
			if( img )
			{
				removeChild( img );
				img = null;
			}
			
			tf = null;
			bt = null;
		}
		
	}
}